package swd.work.generator.v1.java.bean;

import java.io.*;
import java.util.*;

import com.pub.database.*;

import swd.work.generator.v1.java.base.*;

public class ActionBean extends BeanExtend implements BeanImplement {

    public ActionBean(QueryBean pqbnDAO, String pstrUserNO) throws Exception {
        super(pqbnDAO, pstrUserNO);
    }

    /**
     * outputActionMethod
     *
     * @param lbwrPrgNO BufferedWriter
     */
    private void outputActionMethod(BufferedWriter pbwrPrgNO, String pstrSysNO,String pstrPrgNO) throws
            Exception {

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public String action(javax.servlet.jsp.PageContext pageContext){",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "View view=(View)request.getAttribute(\"view\");",
                             2);

        outputCheckLoginCode(pbwrPrgNO,pstrPrgNO);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "String lstrUserNO = request.getSession().getAttribute(\"u_id\").toString();",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());",
                             1);
        /*super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "request.setAttribute(\"qbnDAO\",gqbnDAO);", 1);*/
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "Business business=new Business(gqbnDAO);", 2);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "Flow flow=new Flow(gqbnDAO,lstrUserNO,\"" + pstrSysNO.toUpperCase() +
                             "\",view,business);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String lstrResult=null;", 2);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 2);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getseqno()==null || view.getseqno().trim().equals(\"\")){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setseqno(flow.readFlowSeqno(view,lstrUserNO));", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getseqno()==null || view.getseqno().trim().equals(\"\")){", 2);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setOnloadScript(\"alert('no seqno');window.close();\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 2);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if((view.getPrgAction()!=null && (view.getPrgAction().equals(\"app\") || view.getPrgAction().equals(\"pre_app\")))){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(gqbnDAO.readSingleValueData(\"select r_empno from wkf_jst_eng where flow_num='\" + view.getflow_num() + \"' and jst_flag='N' and r_empno='\" + lstrUserNO + \"'\")==null){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setOnloadScript(\"alert('您非待處理人員!!');\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getflow_num()!=null && (view.getPrgAction()!=null && (view.getPrgAction().equals(\"query\") || view.getPrgAction().equals(\"pre_app\")))){", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "flow.preAddAction(request.getSession().getAttribute(\"u_id\").toString());", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "lstrResult=business.selectTableDataToFld(view);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(lstrResult!=null && !lstrResult.equals(\"\")){", 2);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setOnloadScript(\"alert('\" + lstrResult + \"');\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getPrgAction()!=null && view.getPrgAction().equals(\"add\")){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "flow.preAddAction(lstrUserNO);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setPrgStatus(\"add\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(view.getPrgAction()!=null && view.getPrgAction().equals(\"save\") && view.getPrgStatus().equals(\"add\")){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setOnloadScript(\"alert('\" + flow.addAction() + \"');\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

        outputFlowAppCondition(pbwrPrgNO);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "view.setOnloadScript(\"alert('Program error please contact IT.');\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "finally{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if(gqbnDAO!=null){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "gqbnDAO.close();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "System.out.println(\"can not close querybean in Action.java\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputCheckLoginCode
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     */
    private void outputCheckLoginCode(BufferedWriter pbwrPrgNO,
                                      String pstrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "(new WebFunction()).setPortal_to_session(request); //set session from portal", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "if (request.getSession().getAttribute(\"u_id\")==null) {", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "request.getSession().setAttribute(\"path\",String.valueOf(request.getRequestURI()));", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "request.getSession().setAttribute(\"param\",null);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "response.sendRedirect(\"login.jsp?prg_no=" + pstrPrgNO + ".jsp\" + \"&flow_num=\" + request.getParameter(\"flow_num\")  + \"&portal=\" + request.getParameter(\"portal\") + \"&param=flow_num,portal\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "request.getRequestDispatcher((new StringBuffer(\"login.jsp\")).insert(0,\"/\").toString()).forward(request,response);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"login\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "catch(Exception le){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setOnloadScript(\"can not redirect to login page...please contact IT\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputFlowAppCondition
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputFlowAppCondition(BufferedWriter pbwrPrgNO) {
        Hashtable<String,Hashtable<String,Object>> lhtFlowNO = null;
        Hashtable<String,Object> lhtFlowNOData = null;
        int lintFlowNO_cnt = 0;
        String lstrFlowCode=gstrSysNO.toUpperCase();
        String lstrFlowNO=null;

        try {
            lhtFlowNO = gqbnDAO.hashtableSQL(
                    "select flow_no from floweng_setup where flow_code = '" +
                    lstrFlowCode + "'");

            while (lhtFlowNO != null &&
                   lhtFlowNO.get(String.valueOf(lintFlowNO_cnt)) != null) {

                lhtFlowNOData = (Hashtable<String,Object>) lhtFlowNO.get(String.valueOf(
                        lintFlowNO_cnt));

                lstrFlowNO=lhtFlowNOData.get("FLOW_NO").toString();
                lintFlowNO_cnt++;

                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"if(view.getPrgAction()!=null && view.getPrgAction().equals(\"approve\") && view.getstatus().equals(\"" + lstrFlowNO + "\")){",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"view.setOnloadScript(\"alert('\" + flow.appFlow" + lstrFlowNO + "Action() + \"')\");",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"}",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"if((view.getPrgAction()!=null && view.getPrgAction().equals(\"pre_app\")) && view.getstatus()!=null && view.getstatus().equals(\"" + lstrFlowNO + "\")){",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"flow.preAppFlow" + lstrFlowNO + "();",1);
                super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"}",1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lhtFlowNOData != null) {
                lhtFlowNOData.clear();
            }
            lhtFlowNOData = null;
            if (lhtFlowNO != null) {
                lhtFlowNO.clear();
            }
            lhtFlowNO = null;
        }
    }

    /**
     * outputClassVarible
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputClassVarible(BufferedWriter pbwrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "QueryBean gqbnDAO=null;", 1);
    }

    public String createBean(String pstrPrgNO) {
        String lstrCreateResult = "";
        String lstrBeanFileName = "Action.java";
        BufferedWriter lbwrPrgNO = null;

        try {
            lbwrPrgNO = super.getFwrPrgNO(lstrBeanFileName, "JSP_FRAME_PATH",
                                          "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                          "\\" + pstrPrgNO + "\\", gstrUserNO);

            super.outputBeanHeader(lbwrPrgNO, gstrSysNO,
                                   pstrPrgNO, lstrBeanFileName, gstbTabStr,
                                   gstrTabChar);

            //output Classify varible
            outputClassVarible(lbwrPrgNO);

            //output Action method
            outputActionMethod(lbwrPrgNO, gstrSysNO,pstrPrgNO);

            //output class end single
            super.outputJavaCode(lbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                lbwrPrgNO.flush();
                lbwrPrgNO.close();
                lbwrPrgNO = null;
            } catch (Exception e) {
                System.out.println("cant flush Action.java File!!");
                e.printStackTrace();
            }  
        }
        
        return lstrCreateResult;
    }


}
